import api from "@/util/api";
async function findAll(pageNo = 1, pageSize = 10, params = {}) {
    let resp = await api({
        url: "/user-api/api/v1/user",
        method: "get",
        params: {
            pageNo,
            pageSize,
            ...params
        }
    });
    return resp.data; 
}
//用户组
async function findUserGroup(pageNo = 1, pageSize = 10, params = {}) {
    let resp = await api({
        url: "/rbac-api/api/v1/userGroup/findAll",
        method: "get",
        params: {
            pageNo,
            pageSize,
            ...params
        }
    });
    return resp.data;
}
//用户组新增
async function saveUserGroup(userGroup){
    let resp = await api({
        url:"/rbac-api/api/v1/userGroup/add",
        method:"post",
        data:userGroup,
    })
    return resp;
}
//用户组修改
async function updateUserGroup(userGroup){
    let resp = await api({
        url:"/rbac-api/api/v1/userGroup/update",
        method:"put",
        data:userGroup,
    })
    return resp;
}
//用户组删除多个
async function deleteAllUserGroup(ids){
    let resp = await api({
        url:"/rbac-api/api/v1/userGroup/deleteByIds",
        method:"delete",
        data:ids,
    })
    return resp;
}

async function deleteUserGroupById(id){
    let resp = await api({
        url:"/rbac-api/api/v1/userGroup/deleteById/" + id,
        method:"delete",
    })
    return resp;
}
async function login(account){
    let resp = await api({
        url:"/user-api/api/v1/user/login",
        method:"post",
        data:account
    })
    return resp;
}
async function deleteById(id){
    let resp = await api({
        url:"/user-api/api/v1/user/deleteById",
        method:"delete",
        params:{
            id
        }
    })
    console.log(resp);
    return resp;
}

async function save(user){
    let resp = await api({
        url:"/user-api/api/v1/user/add",
        method:"post",
        data:user,
    })
    return resp;
}

async function updateAdmin(admin){
    let resp = await api({
        url:"/user-api/api/v1/user/update",
        method:"put",
        data:admin,
    })
    return resp;
}
async function updateEnabled(id,enabled){
    let resp = await api({
        url:"/user-api/api/v1/user/updateEnabled",
        method:"put",
        data:{
            id,
            enabled
        }
    })
    return resp;
}
async function updateLocked(id,locked){
    let resp = await api({
        url:"api/v1/user/updateLocked",
        method:"put",
        data:{
            id,
            locked
        }
    })
    return resp;
}
async function findByUsername(){
    let resp = await api({
        url:"/admin/username",
        method:"get",
    })
    return resp;
}
async function findById(id){
    let resp = await api({
        url:"/admin",
        method:"get",
        params:{
            id
        }
    })
    return resp;
}
async function updateRole(id,role){
    let resp = await api({
        url:"/admin/updateRole",
        method:"put",
        params:{
            id,
            role
        }
    })
    return resp;
}
async function deleteAll(ids){
    let resp = await api({
        url:"/user-api/api/v1/user/deleteByIds",
        method:"delete",
        data:ids,
    })
    return resp;
}
async function updateAdminPassword(id, password) {
    let resp = await api({
        url: "/admin/updatePassword",
        method: "put",
        params: {
            id,
            ...password,
        }
    })
    console.log(resp);
    return resp;
}
async function register(data) {
    let resp = await api({
        url: "/admin/register",
        method: "post",
        data
    });
    return resp;
}
export {findAll,deleteById,save,updateAdmin,updateEnabled,findById,updateRole,deleteAll,
    login,findByUsername,updateAdminPassword,register,updateLocked,findUserGroup,saveUserGroup,updateUserGroup,deleteAllUserGroup,deleteUserGroupById};